package com.Maven.impl;

import com.Maven.dao.ItemDao;
import com.Maven.domain.items;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ItemsDaoImpl implements ItemDao {
    public List<items> findAll() {
        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection connection = null;
        List<items> list = new ArrayList<items>();


        //加载驱动类
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/maven?useSSL=false&serverTimezone=UTC","root","123456");
            ps = connection.prepareCall("select * from items");
            rs = ps.executeQuery();

            while (rs.next()){
                items item = new items();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                list.add(item);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                ps.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        //现货区connection对象
        //获取真正操作数据的对象
        //执行查询
        //数据库结果转为java的list集合

        return list;
    }
}
